#
# Copyright (c) 2010 Josef Hardi. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

LoadData <- function(dataset_id) {
  # Load the input data to R workspace.
  #
  # Args:
  #   dataset_id:   The input data set ID named after the directory name
  #                 in which the data are stored.
  #
  base_path<-paste("Documents/Academic/Master/Thesis/Research/DataSet", dataset_id, sep="/")
  average.time<-read.csv(paste(base_path, "dv-avg_solving_time.csv", sep="/"))
  developers<-array(unlist(read.csv(paste(base_path, "developer.csv", sep="/"))))
  experience<-read.csv(paste(base_path, "iv-issue_resolved.csv", sep="/"))
  team.structure<-read.csv(paste(base_path, "iv-team_structure.csv", sep="/"))
  snapshots<-c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)	
}


RegressionProcedures <- function(dataset_id) {
  # Execute the whole experiment procedures.
  #
  base_path<-paste("Documents/Academic/Master/Thesis/Research/DataSet", dataset_id, sep="/")
  average.time<-read.csv(paste(base_path, "dv-avg_solving_time.csv", sep="/"))
  developers<-array(unlist(read.csv(paste(base_path, "developer.csv", sep="/"))))
  experience<-read.csv(paste(base_path, "iv-issue_resolved.csv", sep="/"))
  team.structure<-read.csv(paste(base_path, "iv-team_structure.csv", sep="/"))
  snapshots<-c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)	
    
  source("/Users/Joe/Documents/Academic/Master/Thesis/Research/Scripts/R/learning_curve_regression.R")

  # Experiment 1: FIXED EFFECT
  lambda.1 <- EstimateLambda(average.time, experience, team.structure, developers, snapshots, experiment="fixed")
  dataset.1 <- ConstructLearningDataSet(average.time, experience, team.structure, developers, snapshots, lambda=lambda.1)
  plcurve.1 <- ComputeLearningCurveModel(dataset.1, experiment="fixed")
  
  cat("\n\n")
  cat("==============================================================\n")
  cat("Lambda Regression (1): ", lambda.1, "\n")
  cat("--------------------------------------------------------------\n")
  print(summary(plcurve.1))
  cat("--------------------------------------------------------------\n")
  cat("Variance: ", var(plcurve.1$residuals), "\n")
  cat("--------------------------------------------------------------\n")
  # print(summary(fixef(plcurve.1)))
  cat("--------------------------------------------------------------\n")
  
  # Experiment 2: OLS
  lambda.2 <- EstimateLambda(average.time, experience, team.structure, developers, snapshots, experiment="ols")
  dataset.2 <- ConstructLearningDataSet(average.time, experience, team.structure, developers, snapshots, lambda=lambda.2)
  plcurve.2 <- ComputeLearningCurveModel(dataset.2, experiment="ols")
  
  cat("\n")
  cat("==============================================================\n")  
  cat("Lambda Regression (2): ", lambda.2, "\n")
  cat("--------------------------------------------------------------\n")
  print(summary(plcurve.2))
  cat("--------------------------------------------------------------\n")
  
  # Experiment 3: FIXED EFFECT WITH ROLE
  lambda.3 <- EstimateLambda(average.time, experience, team.structure, developers, snapshots, experiment="with.group")
  dataset.3 <- ConstructLearningDataSet(average.time, experience, team.structure, developers, snapshots, lambda=lambda.3)
  plcurve.3 <- ComputeLearningCurveModel(dataset.3, experiment="with.group")
  
  cat("\n")
  cat("==============================================================\n")
  cat("Lambda Regression (3): ", lambda.3, "\n")
  cat("--------------------------------------------------------------\n")
  print(summary(plcurve.3))
  cat("--------------------------------------------------------------\n")
  # print(summary(fixef(plcurve.3)))
  cat("--------------------------------------------------------------\n")
}